package com.forlove.app.qselect.login.widget;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.forlove.app.R;

/**
 * 启动时的授权页面
 * Created by yjl on 2017/10/2.
 */

public class ErrorMessageView extends RelativeLayout implements View.OnClickListener {
    private View mRootView;
    private Context mContext;
    private TextView message;
    private Handler mHandler;

    public ErrorMessageView(@NonNull Context context) {
        super(context);
        init(context);
    }

    public ErrorMessageView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ErrorMessageView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mRootView = inflater.inflate(R.layout.toast_error_message, this);
        message = (TextView) mRootView.findViewById(R.id.str_error_message);
        mHandler = new Handler();
    }

    public void setError(String error) {
        message.setText(error);
    }

    @Override
    public void onClick(View view) {

    }

    @Override

    public void setVisibility(int visibility) {
        if (isShown() && (visibility == GONE || visibility == INVISIBLE)) {
            Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.dialog_fly_top_out);
            setAnimation(animation);
            animation.start();
        } else if (!isShown() && visibility == VISIBLE) {
            Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.toast_top_in);
            setAnimation(animation);
            animation.start();
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    setVisibility(INVISIBLE);
                }
            }, 3000);
        }
        super.setVisibility(visibility);

    }

    public void showErrorMsg(String msg) {
        message.setText(msg);
        setVisibility(VISIBLE);
    }
}
